package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.domain.SelfTest;
import com.wuwei.elearning.question.domain.SelfTestResult;
import com.wuwei.elearning.question.domain.TestResult;
import com.wuwei.elearning.question.mapper.SelfTestMapper;
import com.wuwei.elearning.question.mapper.SelfTestResultMapper;
import com.wuwei.elearning.question.vo.GetResultVo;
import com.wuwei.elearning.question.vo.PageVo;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自我测试结果表 前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-04-10
 */
@RestController
@CrossOrigin
@RequestMapping("/elearning/selfTestResult")
public class SelfTestResultController {

    @Resource
    private SelfTestMapper selfTestMapper;

    @Resource
    private SelfTestResultMapper selfTestResultMapper;

    /**
     * 分页获取self_test_result的方法
     * 1.先去self_test表根据selectedId获取相应的id
     * 2.再去self_test_result表根据self_test_id获取数据
     * @param selectedId 如果小于10000，则为chapter表中的id字段，如果大于10000，则为subject表的id加上10000
     * @return
     */
    @GetMapping("getList/{selectedId}/{currentPage}/{pageSize}")
    public Result<List<SelfTestResult>> getList(@PathVariable("selectedId") Long selectedId, @PathVariable("currentPage") Integer currentPage,
                                                @PathVariable("pageSize") Integer pageSize,HttpServletRequest request){
        //获取用户名
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        //1.先去self_test表根据selectedId获取相应的id
        LambdaQueryWrapper<SelfTest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SelfTest::getCreateBy,account).eq(SelfTest::getSelectedId,selectedId);
        List<SelfTest> selfTests = selfTestMapper.selectList(queryWrapper);
        List<Long> selfTestIds = new ArrayList<>();
        selfTests.forEach(selfTest -> selfTestIds.add(selfTest.getId()));
        List<SelfTestResult> records = new ArrayList<>();
        if (selfTestIds.size()==0) {
            return Result.ok(records);
        }

        //2.再去self_test_result表根据self_test_id获取数据
        LambdaQueryWrapper<SelfTestResult> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SelfTestResult::getSelfTestId,selfTestIds).orderByDesc(SelfTestResult::getCreateTime);
        IPage<SelfTestResult> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        IPage<SelfTestResult> selfTestResultIPage = selfTestResultMapper.selectPage(page, queryWrapper1);
        records = selfTestResultIPage.getRecords();
        return Result.ok(records);
    }

}

